/**   
 * Copyright © 2016 北京之洲科技有限责任公司. All rights reserved.
 * 
 * @Title: NewsReplyCrawlOfAutoHome.java 
 * @Prject: carfactory
 * @Package: com.zizo.autohome.news.post.crawl 
 * @Description: 
 * @author: Alexis  
 * @email heyan0227@outlook.com
 * @date: 2016年8月15日 下午3:09:13 
 * @version: V1.0   
 */
package com.zizo.autohome.news.reply.crawl;

import java.util.Date;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.zizo.autohome.news.reply.bean.AutoHomeNewsReply;
import com.zizo.autohome.news.reply.dao.NewsReplyDaoOfAutoHome;
import com.zizo.core.Crawl;
import com.zizo.core.DAO;
import com.zizo.util.DocUtil;
import com.zizo.util.Util;

/**
 * @ClassName: NewsReplyCrawlOfAutoHome
 * @Description:
 * @author: Alexis
 * @date: 2016年8月15日 下午3:09:13
 */
public class NewsReplyCrawlOfAutoHome extends Crawl {
	private NewsReplyDaoOfAutoHome newsdao = (NewsReplyDaoOfAutoHome) dao;

	/**
	 * @Title:NewsReplyCrawlOfAutoHome
	 * @Description:
	 * @param id
	 * @param dao
	 */
	public NewsReplyCrawlOfAutoHome(String id, DAO dao) {
		super(id, dao);
	}

	/*
	 * (non Javadoc)
	 * 
	 * @Title: getPages
	 * 
	 * @Description:
	 * 
	 * @param doc
	 * 
	 * @param selectStr
	 * 
	 * @return
	 * 
	 * @see com.zizo.core.CrawlInterface#getPages(org.jsoup.nodes.Document,
	 * java.lang.String)
	 */
	@Override
	public int getPages(Document doc, String selectStr) {
		return 0;
	}

	/*
	 * (non Javadoc)
	 * 
	 * @Title: getItems
	 * 
	 * @Description:
	 * 
	 * @param url
	 * 
	 * @see com.zizo.core.CrawlInterface#getItems(java.lang.String)
	 */
	@Override
	public void getItems(String url) {
		// url= http://www.autohome.com.cn/all/2/#liststart
		Document doc = DocUtil.getDoc(url);
		Elements contents = doc.select("div#auto-channel-lazyload-article ul.article li:not(#ad_tw_04)");
		for (Element content : contents) {
			String link = content.select("a").first().attr("href");
			String tzid = link.split("/")[5].split("\\.")[0].split("-")[0];
			for (int i = 1; i < 101; i++) {
				crawlContent("http://reply.autohome.com.cn/api/comments/show.json?count=50&page=" + i + "&id=" + tzid
						+ "&appid=1&datatype=jsonp&order=0&replyid=0&callback=jQuery17209540401000568037_1471238526155&_=1471238526368");
			}
		}
	}

	/*
	 * (non Javadoc)
	 * 
	 * @Title: crawlContent
	 * 
	 * @Description:
	 * 
	 * @param url
	 * 
	 * @see com.zizo.core.CrawlInterface#crawlContent(java.lang.String)
	 */

	@Override
	public void crawlContent(String url) {
		String body = DocUtil.getJsonDataFromUrl(url);
		String json = body.substring(body.indexOf("(") + 1, body.length() - 1);
		if (json.length() < 100) {
			return;
		}
		try {
			JSONObject contentObj = new JSONObject(json);
			JSONArray contentArr = contentObj.getJSONArray("commentlist");
			for (int i = 0; i < contentArr.length(); i++) {
				JSONObject itemObj = contentArr.getJSONObject(i);
				int replyId = itemObj.getInt("ReplyId");
				int rObjId = itemObj.getInt("RObjId");
				String RReplyDate = itemObj.getString("RReplyDate");
				String rReplyDate = Util.getMatched(RReplyDate, "(/Date\\()(\\d{1,})([+-].*)").group(2);
				String rContent = itemObj.getString("RContent");
				int rTargetReplyId = itemObj.getInt("RTargetReplyId");
				int rMemberId = itemObj.getInt("RMemberId");
				String rMemberName = itemObj.getString("RMemberName");
				int rFloor = itemObj.getInt("RFloor");
				int rMemberSex = itemObj.getInt("RMemberSex");
				int rContentLength = itemObj.getInt("RContentLength");
				String spType = itemObj.getString("SpType");
				AutoHomeNewsReply reply = new AutoHomeNewsReply(replyId, rObjId, rReplyDate, rContent, rTargetReplyId,
						rMemberId, rMemberName, rFloor, rMemberSex, rContentLength, spType);
				newsdao.insertNewsReply(reply);
			}
		} catch (JSONException e) {
			System.out.println(url);
		}
	}

	/*
	 * (non Javadoc)
	 * 
	 * @Title: run
	 * 
	 * @Description:
	 * 
	 * @see java.lang.Runnable#run()
	 */
	@Override
	public void run() {
		this.getItems("http://www.autohome.com.cn/all/" + this.id + "/#liststart");
	}

	public static void main(String[] args) {
		Long time = new Long("1480487134719");
		Date date = new Date(time);
		System.out.println(date);
	}
}
